package com.winit.openapi.pms.v2;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.pms.spi.v2.instance.pscInstance.ProductLogisticsDistService;
import com.winit.pms.spi.v2.instance.pscInstance.command.ProductLogisticsDistByPSCCommand;
import com.winit.pms.spi.v2.instance.pscInstance.command.ProductLogisticsDistCommand;
import com.winit.pms.spi.v2.instance.pscInstance.vo.ProductLogisticsDist;

/**
 * 产品信息
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hui.li2 	1.0  		2015年11月10日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/pms/v2/product")
public class PMSV2ProductController extends BaseController {

    @Resource
    private ProductLogisticsDistService productLogisticsDistService;

    /**
     * 根据条件查询仓库列表
     * 
     * @param 验货仓类型值：Warehouse
     * @param 目的仓类型值：DestWarehouse
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/queryWarehouseList")
    public String queryWarehouseList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg)
                                                                                                                    throws ParseException {
        Object obj = requestMsg.getData();
        ProductLogisticsDistCommand command = new ProductLogisticsDistCommand();
        ProductLogisticsDist locisticsDist = new ProductLogisticsDist();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("productCode")) {
                locisticsDist.setProductCode(json.getString("productCode"));
            }
            if (json.containsKey("distType")) {
                locisticsDist.setLogisticsDistType(json.getString("distType"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setLocisticsDist(locisticsDist);
        Map<String, String> warehouseMap = productLogisticsDistService.queryWarehouseList(command);
        responseMsg.setData(warehouseMap);

        return SUCCESS;
    }

    /**
     * 根据产品编码、起始分区编码、分区类型查询目的分区列表
     * 
     * @param productCode
     * @param distCode
     * @param distType
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/queryDestWarehouseList")
    public String queryDestWarehouseList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg)
                                                                                                                        throws ParseException {
        Object obj = requestMsg.getData();
        ProductLogisticsDistCommand command = new ProductLogisticsDistCommand();
        ProductLogisticsDist locisticsDist = new ProductLogisticsDist();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("productCode")) {
                locisticsDist.setProductCode(json.getString("productCode"));
            }
            if (json.containsKey("distCode")) {
                locisticsDist.setLogisticsDistCode(json.getString("distCode"));
            }
            if (json.containsKey("distType")) {
                locisticsDist.setLogisticsDistType(json.getString("distType"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setLocisticsDist(locisticsDist);
        Map<String, String> warehouseMap = productLogisticsDistService.queryDestWarehouseList(command);
        responseMsg.setData(warehouseMap);

        return SUCCESS;
    }

    /**
     * 根据条件查询sla起始分区列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/queryStartWarehouseListByPSC")
    public String queryStartWarehouseListByPSC(@ModelAttribute RequestMsg requestMsg,
                                               @ModelAttribute ResponseMsg responseMsg) throws ParseException {
        Object obj = requestMsg.getData();
        ProductLogisticsDistByPSCCommand command = new ProductLogisticsDistByPSCCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("pscCode")) {
                command.setPscCode(json.getString("pscCode"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setSlaDate(new Date());
        command.setStartDist(true);
        Map<String, String> warehouseMap = productLogisticsDistService.queryWarehouseListByPSC(command);
        responseMsg.setData(warehouseMap);

        return SUCCESS;
    }

    /**
     * 根据条件查询sla目的分区列表
     * 
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/queryEndWarehouseListByPSC")
    public String queryEndWarehouseListByPSC(@ModelAttribute RequestMsg requestMsg,
                                             @ModelAttribute ResponseMsg responseMsg) throws ParseException {
        Object obj = requestMsg.getData();
        ProductLogisticsDistByPSCCommand command = new ProductLogisticsDistByPSCCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (json.containsKey("pscCode")) {
                command.setPscCode(json.getString("pscCode"));
            }
            if (json.containsKey("startDistCode")) {
                command.setStartDistCode(json.getString("startDistCode"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setSlaDate(new Date());
        command.setStartDist(false);
        Map<String, String> warehouseMap = productLogisticsDistService.queryWarehouseListByPSC(command);
        responseMsg.setData(warehouseMap);

        return SUCCESS;
    }

}
